package com.ruoyi.web.controller.pay;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.pay.StartChargeParam;
import com.ruoyi.framework.web.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.PayCost;
import com.ruoyi.community.service.IPayCostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 缴费设置Controller
 * 
 * @author ruoyi
 * @date 2021-12-14
 */
@RestController
@RequestMapping("/community/cost")
public class PayCostController extends BaseController
{
    @Autowired
    private IPayCostService payCostService;

    /**
     * 发起收费项
     * 接收 costId（收费项Id），typeId（收费项类型） 收费户的数组，
     * @return
     */
    @GetMapping("/start")
    public AjaxResult start(StartChargeParam param) {
        String start = payCostService.start(param);
        return AjaxResult.success(start);
    }

    @GetMapping("/stop/{costId}")
    public AjaxResult stop(@PathVariable Integer costId) {
        String stop = payCostService.stop(costId);
        return AjaxResult.success(stop);
    }
    /**
     * 查询缴费设置列表
     */
    @PreAuthorize("@ss.hasPermi('community:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(PayCost payCost)
    {

        // 只查询该社区缴费列表
        payCost.setCommunityId(SecurityUtils.getCommunityId());
        startPage();
        List<PayCost> list = payCostService.selectPayCostList(payCost);
        return getDataTable(list);
    }

    /**
     * 导出缴费设置列表
     */
    /**
     *
     * @param payCost
     * @return
     */
    @PreAuthorize("@ss.hasPermi('community:cost:export')")
    @Log(title = "缴费设置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(PayCost payCost)
    {
        List<PayCost> list = payCostService.selectPayCostList(payCost);
        ExcelUtil<PayCost> util = new ExcelUtil<PayCost>(PayCost.class);
        return util.exportExcel(list, "缴费设置数据");
    }

    /**
     * 获取缴费设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:cost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(payCostService.selectPayCostById(id));
    }

    /**
     * 新增缴费设置
     */
    @PreAuthorize("@ss.hasPermi('community:cost:add')")
    @Log(title = "缴费设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PayCost payCost)
    {
        // 删除标志设置为 0
        payCost.setDelFlag(false);
        // 添加社区id
        payCost.setCommunityId(SecurityUtils.getCommunityId());
        return toAjax(payCostService.insertPayCost(payCost));
    }

    /**
     * 修改缴费设置
     */
    @PreAuthorize("@ss.hasPermi('community:cost:edit')")
    @Log(title = "缴费设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PayCost payCost)
    {
        return toAjax(payCostService.updatePayCost(payCost));
    }

    /**
     * 删除缴费设置
     */
    @PreAuthorize("@ss.hasPermi('community:cost:remove')")
    @Log(title = "缴费设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(payCostService.deletePayCostByIds(ids));
    }
}
